<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>收银设置管理</title>
    <% include("/common/common_css_js.html"){} %>
</head>
<body>
    <div style="padding-bottom: 5px;">
        <div id="queryForm" class="uc-toolbaroverflow" _borderStyle="border-top:0;">
            <span>部门：</span>
            <input id="queryDepartmentId" name="queryDepartmentId" class="mini-treeselect" style="width:200px;" textField="name" valueField="id" emptyText="请选择..."
                   expandOnLoad="0"   required="false" allowInput="false" showNullItem="true" nullItemText="请选择..."/>

            <span class="separator"></span>

            <% if(so.hasPermission("bCashSet:query")){%>
                <a class="mini-button" iconCls="icon-search" onclick="search()">查询</a>
            <% } %>

            <% if(so.hasPermission("bCashSet:queryCndClear")){%>
                <a class="mini-button" iconCls="icon-new" onclick="clearSearch()">清空查询条件</a>
            <% } %>

            <span class="separator"></span>
            <% if(so.hasPermission("bCashSet:add")){%>
                <a class="mini-button" iconCls="icon-add" onclick="add()">新增</a>
            <% } %>

            <% if(so.hasPermission("bCashSet:edit")){%>
                <a class="mini-button" iconCls="icon-edit" onclick="update()">编辑</a>
            <% } %>

            <% if(so.hasPermission("bCashSet:delete")){%>
                <a class="mini-button" iconCls="icon-remove" onclick="deleteOutUnit()">删除</a>
            <% } %>
        </div>
    </div>

    <div class = "mini-fit">
        <div id="datagrid1" class="mini-datagrid"
             style="width: 100%; height: 100%;"
             url="${base}/pages/cash_set_manage/query" idField="id"
             showPager="true" pageSize="20" sizeList="[10,20,30,50,100]"
             allowResize="true"
             allowCellWrap="true">
            <div property="columns">
<!--                <div field="id" width="140" headerAlign="center" align="left" allowSort="true">ID</div>-->
                <div field="departmentId" width="100" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"department"}' renderer="commonRender">部门</div>
                <div field="startTime" width="100" headerAlign="center" align="center" allowSort="true" dateFormat="yyyy-MM-dd HH:mm"  width="140" headerAlign="center" align="center" allowSort="true" renderer="ondate2Renderer">开始时间</div>
                <div field="endTime" width="100" headerAlign="center" align="center" allowSort="true" dateFormat="yyyy-MM-dd HH:mm"  width="140" headerAlign="center" align="center" allowSort="true" renderer="ondate2Renderer">结束时间</div>
                <div field="casherId" width="100" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysUser"}' renderer="commonRender">收银员</div>
            </div>
        </div>
    </div>

    <!--编辑窗口-->
    <div id="updatePanel" class="mini-window" title="收银设置管理" style="width:700px;height:400px;"
         showMaxButton="true" showCollapseButton="false" showMinButton="true" showShadow="true"
         showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true">
        <div property="footer" style="padding:10px;text-align: right">
            <a  id="insertOrUpdateSaveBtn" name="insertOrUpdateSaveBtn" class="mini-button" iconCls="icon-save" onclick="saveInsertOrUpdate()">保存</a>
        </div>
        <div id="cashSetForm">
            <input id="outUnitId" name="id" class="mini-hidden"/>
            <table style="width: 100%">
                <tr>
                    <td><label>收银部门:</label></td>
                    <td>
                        <input id="departmentId" name="departmentId" class="mini-treeselect" style="width:200px;" textField="name" valueField="id" emptyText="请选择..."
                               expandOnLoad="0"  required="true" allowInput="true" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择收银部门"/>
                    </td>
                    <td><label>收银员:</label></td>
                    <td>
                        <input id="casherId" name="casherId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                               required="true" allowInput="true" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择收银员"/>
                    </td>
                </tr>

                <tr>
                    <td><label>开始时间:</label></td>
                    <td>
                        <input id="startTime" name="startTime" class="mini-datepicker" style="width:200px;" nullValue="null"
                               format="yyyy-MM-dd H:mm" timeFormat="H:mm" showTime="true" showOkButton="true" showClearButton="false"
                               required="true" requiredErrorText="开始时间不能为空" />

                    </td>
                    <td><label>结束时间:</label></td>
                    <td>
                        <input id="endTime" name="endTime" class="mini-datepicker" style="width:200px;" nullValue="null"
                               format="yyyy-MM-dd H:mm" timeFormat="H:mm" showTime="true" showOkButton="true" showClearButton="false"
                               required="true" requiredErrorText="结束时间不能为空" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <script type="text/javascript">
        mini.parse();
        let grid = mini.get("datagrid1");
        let oper;
        let cashSetForm = new mini.Form("#cashSetForm");



        $(function () {

            // 加载列表数据
            grid.load();

            //新增、修改弹出框默认操作
            oper = OPER_ADD();

            let jsonData = commonRenderDropList("department");
            console.log(jsonData);
            mini.get("queryDepartmentId").loadList(jsonData,"id","pid");
            mini.get("departmentId").loadList(public_deepClone(jsonData),"id","pid");

            let casherIdJsonData = commonRenderDropList("sysUser");
            mini.get("casherId").load(casherIdJsonData);

        });

        function search() {
            let form = new mini.Form("#queryForm");
            let data = form.getData(true);
            grid.load(data);
        }

        function clearSearch() {
            let form = new mini.Form("#queryForm");
            form.clear();
        }

        function add(){
            cashSetForm.clear();
            oper = OPER_ADD();

            // 弹出添加窗口
            public_showModelAtPos("updatePanel", "center", "middle");
        }

        function update(){
            cashSetForm.clear();
            // 获取当前选中的行
            let row = grid.getSelected();
            if(!row){
                mini.alert("请选择要编辑的行!","字典编辑",null);
                return;
            }
            if(row.startTime){
                row.startTime = new Date(row.startTime);
            }
            if(row.endTime){
                row.endTime = new Date(row.endTime);
            }
            cashSetForm.setData(row);
            oper = OPER_UPDATE();

            // 弹出添加窗口
            public_showModelAtPos("updatePanel", "center", "middle");
        }

        //新增保存或修改保存
        function saveInsertOrUpdate(){
            // 添加并保存
            cashSetForm.validate();
            if (cashSetForm.isValid() == false){
                public_showErrorTips("请将信息填写完整再进行下一步！");
                return;
            }
            let data = cashSetForm.getData(true);
            data.startTime = new Date(data.startTime);
            data.endTime = new Date(data.endTime);

            let methodName = oper == OPER_ADD() ? "insert" : "update";
            let ret = public_getServerData("${base}/pages/cash_set_manage/" + methodName,data);
            public_showTip(ret);
            if(ret.ok) {
                refreashCache();
                // 重新刷新数据
                grid.load();
                public_onCancel('updatePanel');
            }
        }

        function deleteOutUnit(){
            let select = grid.getSelected();
            if(select == null){
                public_showErrorTips("未选中");
                return;
            }
            mini.confirm(
                "确定删除？",
                "确定？",
                function(action) {
                    if (action == "ok") {
                        let ret = public_getServerData("${base}/pages/cash_set_manage/delete",select.id);
                        public_showTip(ret);
                        if(ret.ok) {
                            refreashCache();
                            // 重新刷新数据
                            grid.removeRow(select, true);
                        }
                    }
                });
        }

        // 刷新缓存，把刷新的缓存放到父页面，应当调用父级页面方法刷新
        function refreashCache() {
            parent.window.loadCache();
        }

    </script>
</body>
</html>